<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>07-数据类型 | 小黑博客</title><meta name="keywords" content="JavaScript,JavaScript高级程序设计第四版"><meta name="author" content="BluesYoung-web"><meta name="copyright" content="BluesYoung-web"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="简单数据类型 Undefined Null Boolean Number String Symbol(ES6 新增)  Undefined 单值 undefined 已声明未初始化，或者变量提升 无论是声明还是未声明，typeof 返回的都是字符串&quot;undefined&quot;  1typeof undefined &#x3D;&#x3D;&#x3D; &amp;#x27;undefined&amp;#x27;   一般来说，永远">
<meta property="og:type" content="article">
<meta property="og:title" content="07-数据类型">
<meta property="og:url" content="https://bluesyoung-web.gitee.io/blog/2020-12-28-07-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.html">
<meta property="og:site_name" content="小黑博客">
<meta property="og:description" content="简单数据类型 Undefined Null Boolean Number String Symbol(ES6 新增)  Undefined 单值 undefined 已声明未初始化，或者变量提升 无论是声明还是未声明，typeof 返回的都是字符串&quot;undefined&quot;  1typeof undefined &#x3D;&#x3D;&#x3D; &amp;#x27;undefined&amp;#x27;   一般来说，永远">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://bluesyoung-web.gitee.io/blog/img/hbs.png">
<meta property="article:published_time" content="2020-12-28T08:28:37.000Z">
<meta property="article:modified_time" content="2021-08-29T11:36:10.270Z">
<meta property="article:author" content="BluesYoung-web">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="JavaScript高级程序设计第四版">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://bluesyoung-web.gitee.io/blog/img/hbs.png"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://bluesyoung-web.gitee.io/blog/2020-12-28-07-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: BluesYoung-web","link":"链接: ","source":"来源: 小黑博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-right"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '07-数据类型',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2021-08-29 19:36:10'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/blog/css/background.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/HexoStaticFile@latest/Hexo/css/custom.min.css"></link><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/lufei_siwangningshi.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">153</div></a></div><div class="data-item"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">54</div></a></div><div class="data-item"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">48</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/blog/img/hbs.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">小黑博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">07-数据类型</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-12-28T08:28:37.000Z" title="发表于 2020-12-28 16:28:37">2020-12-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-29T11:36:10.270Z" title="更新于 2021-08-29 19:36:10">2021-08-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/JavaScript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/">JavaScript高级程序设计</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/JavaScript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/%E7%AC%AC03%E7%AB%A0%E2%80%94%E2%80%94%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/">第03章——语言基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="简单数据类型"><a href="#简单数据类型" class="headerlink" title="简单数据类型"></a>简单数据类型</h2><ul>
<li><code>Undefined</code></li>
<li><code>Null</code></li>
<li><code>Boolean</code></li>
<li><code>Number</code></li>
<li><code>String</code></li>
<li><code>Symbol(ES6 新增)</code></li>
</ul>
<h3 id="Undefined"><a href="#Undefined" class="headerlink" title="Undefined"></a><code>Undefined</code></h3><ul>
<li>单值 <code>undefined</code></li>
<li>已声明未初始化，或者变量提升</li>
<li><strong>无论是声明还是未声明，<code>typeof </code>返回的都是字符串<code>&quot;undefined&quot;</code></strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> <span class="literal">undefined</span> === <span class="string">&#x27;undefined&#x27;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>一般来说，永远不用显式地给某个变量设置<code>undefined</code>值。字面值<code>undefined </code>主要用于比较，而且在 <code>ES3 </code>之前是不存在的。增加这个特殊值的目的就是为了正式明确空对象指针（<code>null</code>）和未初始化变量的区别</p>
</blockquote>
<blockquote>
<p>即使未初始化的变量会被自动赋予 <code>undefined </code>值，但我们仍然建议在声明变量的同时进行初始化。这样，当 <code>typeof </code>返回<code>&quot;undefined&quot;</code>时，你就会知道那是因为给定的变量尚未声明，而不是声明了但未初始化</p>
</blockquote>
<h3 id="Null"><a href="#Null" class="headerlink" title="Null"></a><code>Null</code></h3><ul>
<li>单值 <code>null</code></li>
<li>逻辑上讲，<code>null</code>值表示一个空对象指针</li>
<li><code>undefined</code>派生自<code>null</code>，<code>undefined == null</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> <span class="literal">null</span> === <span class="string">&#x27;object&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="Boolean"><a href="#Boolean" class="headerlink" title="Boolean"></a><code>Boolean</code></h3><p>- <code>true</code> <code>false</code></p>
<p>- 类型转换<code>Boolean()</code></p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>true</th>
<th>false</th>
</tr>
</thead>
<tbody><tr>
<td>Boolean</td>
<td><code>true</code></td>
<td><code>false</code></td>
</tr>
<tr>
<td>String</td>
<td>非空字符串</td>
<td>空字符串</td>
</tr>
<tr>
<td>Number</td>
<td>非零，包含<strong>负数</strong>和无穷大</td>
<td>0，<code>NaN</code></td>
</tr>
<tr>
<td>Object</td>
<td>任意对象</td>
<td><code>null</code></td>
</tr>
<tr>
<td>Undefined</td>
<td>不能存在</td>
<td><code>undefined</code></td>
</tr>
</tbody></table>
<h3 id="Number"><a href="#Number" class="headerlink" title="Number"></a><code>Number</code></h3><p>- 默认十进制</p>
<p>- 八进制<code>0</code>开头，无效值转为十进制（<strong>严格模式无效</strong>，<code>ES6</code>使用<code>0o</code>）</p>
<p>- 十六进制<code>0x</code>开头，无效转为十进制(<strong>前缀区分大小写，数值不区分大小写</strong>)</p>
<p>- <strong>算术计算时都将转为十进制</strong></p>
<p>- 最小值<code>Number.MIN_VALUE</code></p>
<p>- 最大值<code>Number.MAX_VALUE</code></p>
<p>- 正无穷<code>Number.POSITIVE_INFINITY</code></p>
<p>- 负无穷<code>Number.NEGATIVE_INFINITY</code></p>
<p>- 判断是否无穷大<code>Number.isFinite(num)</code></p>
<h4 id="浮点数"><a href="#浮点数" class="headerlink" title="浮点数"></a>浮点数</h4><ul>
<li>数值中必须包含小数点，且小数点后至少有一位数字</li>
<li>小数点之前可以不加数字，但推荐加上</li>
<li>浮点数所占用的内存空间是整数的两倍，所以<code>1.</code>和<code>1.0</code>之类的数值都会被转换为整数</li>
<li>科学记数法 <code>1000 === 1e3</code>，默认情况下，<code>ECMAScript </code>会将小数点后至少包含 6 个零的浮点值转换为科学记数法</li>
<li>浮点值的精确度最高可达 <strong>17</strong> 位小数，但在算术计算中远不如整数精确。例如，<strong>0.1 加 0.2 得到的不 是 0.3，而是 0.300 000 000 000 000 04</strong></li>
</ul>
<h4 id="NaN"><a href="#NaN" class="headerlink" title="NaN"></a><code>NaN</code></h4><ul>
<li><p>Not a Number</p>
</li>
<li><p><code>NaN</code>参与的运算结果都是<code>NaN</code></p>
</li>
<li><p>狠起来连自己都怕<code>NaN != NaN</code></p>
</li>
<li><p>判断是否<code>NaN</code>，<code>Number.isNaN()</code></p>
<p>会先对参数进行数值转换<code>Number()</code>，转换失败返回<code>true</code>，转换成功返回<code>false</code></p>
</li>
</ul>
<h4 id="Number-1"><a href="#Number-1" class="headerlink" title="Number"></a>Number</h4><table>
<thead>
<tr>
<th align="left">值</th>
<th align="left">转换结果</th>
</tr>
</thead>
<tbody><tr>
<td align="left">true</td>
<td align="left">1</td>
</tr>
<tr>
<td align="left">false,null,’’</td>
<td align="left">0</td>
</tr>
<tr>
<td align="left">number</td>
<td align="left">number</td>
</tr>
<tr>
<td align="left">undefined</td>
<td align="left"><code>NaN</code></td>
</tr>
<tr>
<td align="left">纯数值字符串</td>
<td align="left">去掉引号（十进制，八进制会被当成十进制）</td>
</tr>
<tr>
<td align="left">十六进制字符串</td>
<td align="left">转为十进制的值</td>
</tr>
<tr>
<td align="left">其余字符串</td>
<td align="left"><code>NaN</code></td>
</tr>
<tr>
<td align="left">Object</td>
<td align="left">先调用<code>valueOf()</code>方法，转换返回值；如果为<code>NaN</code>，调用<code>toString()</code>方法，转换返回值，返回</td>
</tr>
</tbody></table>
<h4 id="parseInt"><a href="#parseInt" class="headerlink" title="parseInt"></a><code>parseInt</code></h4><ul>
<li><code>parseInt(转换值, [进制: 2, 8, 10, 16]);</code></li>
<li>忽略字符串前部的空格，直到第一个非空字符</li>
<li>如果第一个非空字符不是数字字符或负号则返回<code>NaN</code></li>
<li>如果第一个字符是数字字符则继续解析直至结束或遇到非数字字符</li>
<li><strong>可以解析八进制，最好显示说明，说明之后可不带进制符</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;&#x27;</span>) =&gt; <span class="literal">NaN</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;1234sgfakf&#x27;</span>) =&gt; <span class="number">1234</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;0xa&#x27;</span>) =&gt; <span class="number">10</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="number">22.5</span>) =&gt; <span class="number">22</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="number">070</span>) =&gt; <span class="number">56</span></span><br></pre></td></tr></table></figure>

<h4 id="parseFloat"><a href="#parseFloat" class="headerlink" title="parseFloat"></a><code>parseFloat</code></h4><ul>
<li><strong>只能转换十进制，16 进制始终返回 0</strong></li>
<li><strong>始终忽略前导零</strong></li>
<li><strong>只有第一个小数点有效</strong></li>
<li>没有小数点或小数点之后都是零则返回整数</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">parseFloat</span>(<span class="string">&#x27;1234dsgk&#x27;</span>) =&gt; <span class="number">1234</span></span><br><span class="line"><span class="built_in">parseFloat</span>(<span class="string">&#x27;0xa&#x27;</span>) =&gt; <span class="number">0</span></span><br><span class="line"><span class="built_in">parseFloat</span>(<span class="string">&#x27;22.5&#x27;</span>) =&gt; <span class="number">22.5</span></span><br><span class="line"><span class="built_in">parseFloat</span>(<span class="string">&#x27;12.34.56&#x27;</span>) =&gt; <span class="number">12.34</span></span><br><span class="line"><span class="built_in">parseFloat</span>(<span class="string">&#x27;0448.99&#x27;</span>) =&gt; <span class="number">448.99</span></span><br></pre></td></tr></table></figure>

<h3 id="String"><a href="#String" class="headerlink" title="String"></a><code>String</code></h3><ul>
<li>表示零个或多个 16 位的 <code>Unicode</code> 字符序列</li>
<li>可以使用单引号、双引号、反引号定义，但一定要匹配</li>
<li>字符串不可变，修改 === 销毁原始字符串，保存新的字符串到变量</li>
</ul>
<table>
<thead>
<tr>
<th align="left">字面量</th>
<th align="left">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>\n</code></td>
<td align="left">换行符</td>
</tr>
<tr>
<td align="left"><code>\t</code></td>
<td align="left">制表符</td>
</tr>
<tr>
<td align="left"><code>\b</code></td>
<td align="left">空格/退格</td>
</tr>
<tr>
<td align="left"><code>\r</code></td>
<td align="left">回车</td>
</tr>
<tr>
<td align="left"><code>\f</code></td>
<td align="left">进纸/换页</td>
</tr>
<tr>
<td align="left"><code>\\</code></td>
<td align="left">\</td>
</tr>
<tr>
<td align="left"><code>\&#39;</code></td>
<td align="left">‘</td>
</tr>
<tr>
<td align="left"><code>\&quot;</code></td>
<td align="left">“</td>
</tr>
<tr>
<td align="left"><code>\反引号</code></td>
<td align="left">`</td>
</tr>
<tr>
<td align="left"><code>\xnn</code></td>
<td align="left">两个十六位进制表示字符</td>
</tr>
<tr>
<td align="left"><code>\unnnn</code></td>
<td align="left">四个十六进制位表示字符</td>
</tr>
</tbody></table>
<h4 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString()"></a><code>toString()</code></h4><ul>
<li>除了null和undefined之外都有的方法</li>
<li>数值调用时可选参数为进制数，默认十进制</li>
</ul>
<h4 id="String-1"><a href="#String-1" class="headerlink" title="String()"></a><code>String()</code></h4><ul>
<li>如果有<code>toString()</code>方法，则返回其调用结果</li>
<li><code>null =&gt; &#39;null&#39;</code></li>
<li><code>undefined =&gt; &#39;undefined&#39;</code></li>
</ul>
<h4 id="模板字符串"><a href="#模板字符串" class="headerlink" title="模板字符串"></a>模板字符串</h4><ul>
<li>使用反引号包裹</li>
<li>内部可保留换行符</li>
<li>可通过<code>$&#123;&#125;</code>进行插值，大括号内部可放入变量或者表达式</li>
<li>所有插入的值都会使用<code>toString()</code>强制转化为字符串</li>
</ul>
<h5 id="标签函数"><a href="#标签函数" class="headerlink" title="标签函数"></a>标签函数</h5><ul>
<li>标签函数会接收被插值记号分隔后的模板和对每个表达式求值的结果</li>
<li>标签函数本身是一个常规函数，通过<strong>前缀到模板字面量</strong>来应用自定义行为</li>
<li>标签函数接收到的<strong>参数</strong>依次是<strong>原始字符串数组</strong>和<strong>对每个表达式求值的结果</strong></li>
<li>函数的返回值就是模板字符串的最终值</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="number">6</span>, b = <span class="number">9</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">tag</span>(<span class="params">str, ...arr</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(str);</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;-------------------&#x27;</span>);</span><br><span class="line">    arr.forEach(<span class="function">(<span class="params">v</span>) =&gt;</span> <span class="built_in">console</span>.log(v));</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;-------------------&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;我是标签函数的返回值&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> formal = <span class="string">`<span class="subst">$&#123;a&#125;</span> + <span class="subst">$&#123;b&#125;</span> = <span class="subst">$&#123;a + b&#125;</span>`</span>;</span><br><span class="line"><span class="keyword">const</span> unFormal = tag<span class="string">`<span class="subst">$&#123;a&#125;</span> + <span class="subst">$&#123;b&#125;</span> = <span class="subst">$&#123;a + b&#125;</span>`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(formal);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;-------------------&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(unFormal);</span><br><span class="line"><span class="comment">/////////////////////////////////////////////</span></span><br><span class="line">[<span class="string">&quot;&quot;</span>, <span class="string">&quot; + &quot;</span>, <span class="string">&quot; = &quot;</span>, <span class="string">&quot;&quot;</span>, <span class="attr">raw</span>: <span class="built_in">Array</span>(<span class="number">4</span>)]</span><br><span class="line">-------------------</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="number">15</span></span><br><span class="line">-------------------</span><br><span class="line"><span class="number">6</span> + <span class="number">9</span> = <span class="number">15</span></span><br><span class="line">-------------------</span><br><span class="line">我是标签函数的返回值</span><br></pre></td></tr></table></figure>

<h5 id="获取字符串的原始值"><a href="#获取字符串的原始值" class="headerlink" title="获取字符串的原始值"></a>获取字符串的原始值</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">`\u00A9`</span>); <span class="comment">// ©</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">String</span>.raw<span class="string">`\u00A9`</span>); <span class="comment">// \u00A9 </span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">`first line\nsecond line`</span>);</span><br><span class="line"><span class="comment">// first line</span></span><br><span class="line"><span class="comment">// second line</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">String</span>.raw<span class="string">`first line\nsecond line`</span>); <span class="comment">// &quot;first line\nsecond line&quot; </span></span><br></pre></td></tr></table></figure>

<h3 id="Symbol"><a href="#Symbol" class="headerlink" title="Symbol"></a><code>Symbol</code></h3><ul>
<li><code>ES6</code> 新增数据类型</li>
<li>符号实例是<strong>唯一的、不可变的</strong></li>
<li>用于确保对象属性使用唯一标识符，不会发生属性冲突</li>
<li><strong>直接作为函数调用，不可使用 <code>new</code> 操作符</strong></li>
<li>调用时可传入对于符号的描述，但与符号定义或标识完全无关</li>
<li>因为符号属性是对内存中符号的一个引用，所以直接创建并用作属性的符号不会丢失。但是，如果没有<strong>显式地保存</strong>对这些属性的引用，那么必须遍历对象的所有符号属性才能找到相应的属性键</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> genericSymbol = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">let</span> otherGenericSymbol = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="built_in">console</span>.log(genericSymbol == otherGenericSymbol); <span class="comment">// false </span></span><br><span class="line"><span class="keyword">let</span> fooSymbol = <span class="built_in">Symbol</span>(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> otherFooSymbol = <span class="built_in">Symbol</span>(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(fooSymbol == otherFooSymbol); <span class="comment">// false </span></span><br><span class="line"><span class="keyword">typeof</span> genericSymbol === <span class="string">&#x27;symbol&#x27;</span></span><br></pre></td></tr></table></figure>

<h4 id="Symbol-for"><a href="#Symbol-for" class="headerlink" title="Symbol.for()"></a><code>Symbol.for()</code></h4><ul>
<li>如果运行时的不同部分需要<strong>共享</strong>和<strong>重用</strong>符号实例，那么可以用一个字符串作为键，在全局符号注册表中创建并重用符号</li>
<li>即使采用相同的符号描述，在全局注册表中定义的符号跟使用 <code>Symbol()</code>定义的符号也并不等同</li>
<li>全局注册表中的符号<strong>必须使用字符串键来创建</strong>，因此作为参数传给<code> Symbol.for()</code>的任何值都会被转换为字符串</li>
<li>注册表中使用的键同时也会被用作符号描述</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fooGlobalSymbol = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>); <span class="comment">// 创建新符号</span></span><br><span class="line"><span class="keyword">let</span> otherFooGlobalSymbol = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>); <span class="comment">// 重用已有符号</span></span><br><span class="line"><span class="built_in">console</span>.log(fooGlobalSymbol === otherFooGlobalSymbol); <span class="comment">// true </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> localSymbol = <span class="built_in">Symbol</span>(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> globalSymbol = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(localSymbol === globalSymbol); <span class="comment">// false </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> emptyGlobalSymbol = <span class="built_in">Symbol</span>.for();</span><br><span class="line"><span class="built_in">console</span>.log(emptyGlobalSymbol); <span class="comment">// Symbol(undefined) </span></span><br></pre></td></tr></table></figure>

<h4 id="Symbol-keyFor"><a href="#Symbol-keyFor" class="headerlink" title="Symbol.keyFor()"></a><code>Symbol.keyFor()</code></h4><ul>
<li>查询全局注册表，这个方法接收符号，返回该全局符号对应的字符串键</li>
<li>如果查询的不是全局符号，则返回 <code>undefined</code></li>
<li>如果传给<code> Symbol.keyFor()</code>的不是符号，则该方法抛出 <code>TypeError</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建全局符号</span></span><br><span class="line"><span class="keyword">let</span> s = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;foo&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Symbol</span>.keyFor(s)); <span class="comment">// foo </span></span><br><span class="line"><span class="comment">// 创建普通符号</span></span><br><span class="line"><span class="keyword">let</span> s2 = <span class="built_in">Symbol</span>(<span class="string">&#x27;bar&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Symbol</span>.keyFor(s2)); <span class="comment">// undefined </span></span><br><span class="line"><span class="comment">// 类型不对</span></span><br><span class="line"><span class="built_in">Symbol</span>.keyFor(<span class="number">123</span>); <span class="comment">// TypeError: 123 is not a symbol </span></span><br></pre></td></tr></table></figure>

<h4 id="获取对象属性"><a href="#获取对象属性" class="headerlink" title="获取对象属性"></a>获取对象属性</h4><ul>
<li><code>Object.getOwnPropertyNames()</code>返回对象实例的<strong>常规属性</strong>数组</li>
<li><code>Object.getOwnPropertySymbols()</code>返回对象实例的<strong>符号属性</strong>数组</li>
<li><code>Object.getOwnPropertyDescriptors()</code>会返回同时包含<strong>常规和符号属性描述符</strong>的对象</li>
<li><code>Reflect.ownKeys()</code>会返回<strong>两种类型</strong>的键数组</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s1 = <span class="built_in">Symbol</span>(<span class="string">&#x27;foo&#x27;</span>),</span><br><span class="line"> s2 = <span class="built_in">Symbol</span>(<span class="string">&#x27;bar&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> o = &#123;</span><br><span class="line"> [s1]: <span class="string">&#x27;foo val&#x27;</span>,</span><br><span class="line"> [s2]: <span class="string">&#x27;bar val&#x27;</span>,</span><br><span class="line"> baz: <span class="string">&#x27;baz val&#x27;</span>,</span><br><span class="line"> qux: <span class="string">&#x27;qux val&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertySymbols(o));</span><br><span class="line"><span class="comment">// [Symbol(foo), Symbol(bar)]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertyNames(o));</span><br><span class="line"><span class="comment">// [&quot;baz&quot;, &quot;qux&quot;]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertyDescriptors(o));</span><br><span class="line"><span class="comment">// &#123;baz: &#123;...&#125;, qux: &#123;...&#125;, Symbol(foo): &#123;...&#125;, Symbol(bar): &#123;...&#125;&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Reflect</span>.ownKeys(o));</span><br><span class="line"><span class="comment">// [&quot;baz&quot;, &quot;qux&quot;, Symbol(foo), Symbol(bar)] </span></span><br></pre></td></tr></table></figure>

<h4 id="常用内置符号"><a href="#常用内置符号" class="headerlink" title="常用内置符号"></a>常用内置符号</h4><ul>
<li>用于暴露语言内部行为，开发者可以直接访问、重写或模拟这些行为</li>
<li>以 <code>Symbol </code>工厂函数字符串属性的形式存在</li>
</ul>
<style>
  table th:nth-of-type(1) {
    width: 190px;
  }
</style>



<table>
<thead>
<tr>
<th>name</th>
<th>type</th>
<th>des</th>
</tr>
</thead>
<tbody><tr>
<td><code>iterator</code></td>
<td>Function</td>
<td><code>for-of</code>迭代器<code>API</code>函数</td>
</tr>
<tr>
<td><code>asyncIterator</code></td>
<td>Function</td>
<td><code>for-await-of</code>异步迭代器<code>API</code>函数</td>
</tr>
<tr>
<td><code>hasInstance</code></td>
<td>Function</td>
<td>决定<code>instanceof</code>结果的函数</td>
</tr>
<tr>
<td><code>isConcatSpreadable</code></td>
<td>Boolean</td>
<td>使用<code>Array.prototype.concat()</code>拼接时，是否拆解其数组元素（合并/或整体压入）</td>
</tr>
<tr>
<td><code>match</code></td>
<td>Function</td>
<td>由 <code>String.prototype.match()</code>方法使用</td>
</tr>
<tr>
<td><code>replace</code></td>
<td>Function</td>
<td>由<code> String.prototype.replace()</code>方法使用</td>
</tr>
<tr>
<td><code>search</code></td>
<td>Function</td>
<td>由 <code>String.prototype.search()</code>方法使用</td>
</tr>
<tr>
<td><code>species</code></td>
<td>Function</td>
<td><strong>作为创建派生对象的构造函数</strong>,用<code> Symbol.species</code> 定义静态的获取器（<code>getter</code>）方法，<strong>可以覆盖新创建实例的原型定义</strong></td>
</tr>
<tr>
<td><code>split</code></td>
<td>Function</td>
<td>由 <code>String.prototype.split()</code>方法使用</td>
</tr>
<tr>
<td><code>toPrimitive</code></td>
<td>Function</td>
<td>将对象转换为相应的原始值（<strong>隐式转换</strong>）。由 <code>ToPrimitive </code>抽象操作使用</td>
</tr>
<tr>
<td><code>toStringTag</code></td>
<td>Function</td>
<td>由内置方法<code> Object.prototype.toString()</code>使用</td>
</tr>
<tr>
<td><code>unscopables</code></td>
<td>Object</td>
<td>该对象所有的以及继承的属性， 都会从关联对象的 <code>with </code>环境绑定中排除，<strong>不推荐使用</strong></td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> initial = [<span class="string">&#x27;foo&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> array = [<span class="string">&#x27;bar&#x27;</span>];</span><br><span class="line"><span class="built_in">console</span>.log(array[<span class="built_in">Symbol</span>.isConcatSpreadable]); <span class="comment">// undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(array)); <span class="comment">// [&#x27;foo&#x27;, &#x27;bar&#x27;]</span></span><br><span class="line">array[<span class="built_in">Symbol</span>.isConcatSpreadable] = <span class="literal">false</span>;</span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(array)); <span class="comment">// [&#x27;foo&#x27;, Array(1)] </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arrayLikeObject = &#123; <span class="attr">length</span>: <span class="number">1</span>, <span class="number">0</span>: <span class="string">&#x27;baz&#x27;</span> &#125;;</span><br><span class="line"><span class="built_in">console</span>.log(arrayLikeObject[<span class="built_in">Symbol</span>.isConcatSpreadable]); <span class="comment">// undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(arrayLikeObject)); <span class="comment">// [&#x27;foo&#x27;, &#123;...&#125;]</span></span><br><span class="line">arrayLikeObject[<span class="built_in">Symbol</span>.isConcatSpreadable] = <span class="literal">true</span>;</span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(arrayLikeObject)); <span class="comment">// [&#x27;foo&#x27;, &#x27;baz&#x27;] </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> otherObject = <span class="keyword">new</span> <span class="built_in">Set</span>().add(<span class="string">&#x27;qux&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(otherObject[<span class="built_in">Symbol</span>.isConcatSpreadable]); <span class="comment">// undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(otherObject)); <span class="comment">// [&#x27;foo&#x27;, Set(1)]</span></span><br><span class="line">otherObject[<span class="built_in">Symbol</span>.isConcatSpreadable] = <span class="literal">true</span>;</span><br><span class="line"><span class="built_in">console</span>.log(initial.concat(otherObject)); <span class="comment">// [&#x27;foo&#x27;] </span></span><br><span class="line"><span class="comment">////////////////////////////////////</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Baz</span> <span class="keyword">extends</span> <span class="title">Array</span> </span>&#123;</span><br><span class="line"> <span class="keyword">static</span> get [<span class="built_in">Symbol</span>.species]() &#123;</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">Array</span>;</span><br><span class="line"> &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">let</span> baz = <span class="keyword">new</span> Baz();</span><br><span class="line"><span class="built_in">console</span>.log(baz <span class="keyword">instanceof</span> <span class="built_in">Array</span>); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(baz <span class="keyword">instanceof</span> Baz); <span class="comment">// true</span></span><br><span class="line">baz = baz.concat(<span class="string">&#x27;baz&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(baz <span class="keyword">instanceof</span> <span class="built_in">Array</span>); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(baz <span class="keyword">instanceof</span> Baz); <span class="comment">// false </span></span><br><span class="line"><span class="comment">/////////////////////////////////////</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bar</span> </span>&#123;</span><br><span class="line"> <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">     <span class="built_in">this</span>[<span class="built_in">Symbol</span>.toPrimitive] = <span class="function"><span class="keyword">function</span>(<span class="params">hint</span>) </span>&#123;</span><br><span class="line">         <span class="keyword">switch</span> (hint) &#123;</span><br><span class="line">             <span class="keyword">case</span> <span class="string">&#x27;number&#x27;</span>:</span><br><span class="line">             <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">             <span class="keyword">case</span> <span class="string">&#x27;string&#x27;</span>:</span><br><span class="line">             <span class="keyword">return</span> <span class="string">&#x27;string bar&#x27;</span>;</span><br><span class="line">             <span class="keyword">case</span> <span class="string">&#x27;default&#x27;</span>:</span><br><span class="line">             <span class="keyword">default</span>:</span><br><span class="line">             <span class="keyword">return</span> <span class="string">&#x27;default bar&#x27;</span>;</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">let</span> bar = <span class="keyword">new</span> Bar();</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">3</span> + bar); <span class="comment">// &quot;3default bar&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">3</span> - bar); <span class="comment">// 0</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">String</span>(bar)); <span class="comment">// &quot;string bar&quot; </span></span><br></pre></td></tr></table></figure>



<h2 id="复杂数据类型"><a href="#复杂数据类型" class="headerlink" title="复杂数据类型"></a>复杂数据类型</h2><ul>
<li><code>Object</code></li>
</ul>
<h3 id="typeof-操作符"><a href="#typeof-操作符" class="headerlink" title="typeof 操作符"></a><code>typeof</code> 操作符</h3><ul>
<li>使用 <code>typeof &#39;str&#39;</code> or <code>typeof(&#39;str&#39;)</code></li>
<li>返回值：<ul>
<li><code>&#39;undefined&#39;</code> -&gt; 未定义</li>
<li><code>&#39;boolean&#39;</code> -&gt; 布尔值</li>
<li><code>&#39;string&#39;</code> -&gt; 字符串</li>
<li><code>&#39;number&#39;</code> -&gt; 数值</li>
<li><code>&#39;object&#39;</code> -&gt; <strong>对象或者<code>null</code></strong></li>
<li><code>&#39;function&#39;</code> -&gt; 函数</li>
<li><code>&#39;symbol&#39;</code> -&gt; 符号</li>
</ul>
</li>
</ul>
<blockquote>
<p>严格来讲，函数在 <code>ECMAScript</code> 中被认为是对象，并不代表一种数据类型。可是，函数也有自己特殊的属性。为此，就有必要通过<code>typeof</code>操作符来区分函数和其他对象</p>
</blockquote>
<h3 id="Object"><a href="#Object" class="headerlink" title="Object"></a><code>Object</code></h3><ul>
<li>一组数据和功能的集合</li>
<li>可以通过创建 <code>Object </code>类型的实例来创建自己的对象，然后再给对象添加属性和方法</li>
<li><code>const obj = new Object();</code>不带括号也合法，但<strong>不推荐</strong></li>
<li><code>const obj = &#123;&#125;;</code>字面量直接创建</li>
</ul>
<h4 id="实例的方法及属性"><a href="#实例的方法及属性" class="headerlink" title="实例的方法及属性"></a>实例的方法及属性</h4><ul>
<li><code>constructor</code>用于创建当前对象实例的构造函数</li>
<li><code>hasOwnProperty(propertyName)</code>判断当前对象<strong>实例（非原型）</strong>上是否存在给定的属性</li>
<li><code>isPrototypeOf(object)</code>判断当前对象是否为另一个对象的原型</li>
<li><code>propertyIsEnumerable(propertyName)</code>判断给定属性是否可以通过<code>for-in</code>遍历</li>
<li><code>toLocaleString()</code>：返回对象的字符串表示，该字符串反映对象所在的本地化执行环境</li>
<li><code>toString()</code>：返回对象的字符串表示</li>
<li><code>valueOf()</code>：返回对象对应的字符串、数值或布尔值表示。通常与<code> toString()</code>的返回值相同</li>
<li>因为在 <code>ECMAScript </code>中 <code>Object </code>是所有对象的基类，所以任何对象都有这些属性和方法</li>
</ul>
<blockquote>
<p>严格来讲，<code>ECMA-262</code> 中对象的行为不一定适合 <code>JavaScript </code>中的其他对象。比如浏 览器环境中的 <code>BOM </code>和 <code>DOM </code>对象，都是由宿主环境定义和提供的宿主对象。而宿主对象不受 <code>ECMA-262</code> 约束，所以它们可能会也可能不会继承 <code>Object</code></p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">BluesYoung-web</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://bluesyoung-web.gitee.io/blog/2020-12-28-07-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.html">https://bluesyoung-web.gitee.io/blog/2020-12-28-07-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://BluesYoung-web.gitee.io/blog" target="_blank">小黑博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/blog/tags/JavaScript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E7%AC%AC%E5%9B%9B%E7%89%88/">JavaScript高级程序设计第四版</a></div><div class="post_share"></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wx.png" target="_blank"><img class="post-qr-code-img" src="/blog/img/wx.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/zfb.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/zfb.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2020-12-28-01-历史回顾.html" title="01-历史回顾"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">01-历史回顾</div></div></a></div><div><a href="/blog/2020-12-28-03-script元素.html" title="03-script元素"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">03-script元素</div></div></a></div><div><a href="/blog/2020-12-28-02-实现.html" title="02-实现"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">02-实现</div></div></a></div><div><a href="/blog/2020-12-28-04-文档模式.html" title="04-文档模式"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">04-文档模式</div></div></a></div><div><a href="/blog/2020-12-28-05-语法.html" title="05-语法"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">05-语法</div></div></a></div><div><a href="/blog/2020-12-28-06-变量声明.html" title="06-变量声明"><img class="cover" src="/blog/img/hbs.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-12-28</div><div class="title">06-变量声明</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/lufei_siwangningshi.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">BluesYoung-web</div><div class="author-info__description">一枚不善言辞的技术宅</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">153</div></a></div><div class="card-info-data-item"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">54</div></a></div><div class="card-info-data-item"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">48</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/BluesYoung-web"><i class="fab fa-gitee"></i><span>点个小星星吧</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/BluesYoung-web" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:15171255945@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-24-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E8%BD%AF%E9%94%AE%E7%9B%98%E5%AF%BC%E8%87%B4%E5%B8%83%E5%B1%80%E9%94%99%E4%B9%B1.html" title="移动端软键盘导致布局错乱"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="移动端软键盘导致布局错乱"/></a><div class="content"><a class="title" href="/blog/2021-08-24-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E8%BD%AF%E9%94%AE%E7%9B%98%E5%AF%BC%E8%87%B4%E5%B8%83%E5%B1%80%E9%94%99%E4%B9%B1.html" title="移动端软键盘导致布局错乱">移动端软键盘导致布局错乱</a><time datetime="2021-08-24T10:01:20.000Z" title="发表于 2021-08-24 18:01:20">2021-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-16-Linux%E5%AE%89%E8%A3%85you-get.html" title="Linux安装you-get"><img src="/blog/img/shumeipai_cover.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux安装you-get"/></a><div class="content"><a class="title" href="/blog/2021-08-16-Linux%E5%AE%89%E8%A3%85you-get.html" title="Linux安装you-get">Linux安装you-get</a><time datetime="2021-08-16T08:07:31.000Z" title="发表于 2021-08-16 16:07:31">2021-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-16-git%E5%88%A0%E9%99%A4%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E6%8F%90%E4%BA%A4.html" title="git删除远程仓库的提交"><img src="/blog/img/skill.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="git删除远程仓库的提交"/></a><div class="content"><a class="title" href="/blog/2021-08-16-git%E5%88%A0%E9%99%A4%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E6%8F%90%E4%BA%A4.html" title="git删除远程仓库的提交">git删除远程仓库的提交</a><time datetime="2021-08-16T07:52:02.000Z" title="发表于 2021-08-16 15:52:02">2021-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-13-%E5%B0%86pad%E6%89%A9%E5%B1%95%E4%B8%BA%E7%94%B5%E8%84%91%E7%9A%84%E5%89%AF%E5%B1%8F.html" title="将pad扩展为电脑的副屏"><img src="/blog/img/skill.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="将pad扩展为电脑的副屏"/></a><div class="content"><a class="title" href="/blog/2021-08-13-%E5%B0%86pad%E6%89%A9%E5%B1%95%E4%B8%BA%E7%94%B5%E8%84%91%E7%9A%84%E5%89%AF%E5%B1%8F.html" title="将pad扩展为电脑的副屏">将pad扩展为电脑的副屏</a><time datetime="2021-08-13T11:44:04.000Z" title="发表于 2021-08-13 19:44:04">2021-08-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-11-Linux%E5%90%AF%E7%94%A8SSH%E6%9C%8D%E5%8A%A1.html" title="Linux启用SSH服务"><img src="/blog/img/shumeipai_cover.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux启用SSH服务"/></a><div class="content"><a class="title" href="/blog/2021-08-11-Linux%E5%90%AF%E7%94%A8SSH%E6%9C%8D%E5%8A%A1.html" title="Linux启用SSH服务">Linux启用SSH服务</a><time datetime="2021-08-11T10:50:04.000Z" title="发表于 2021-08-11 18:50:04">2021-08-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By BluesYoung-web</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script> <script src="/blog/js/xkTool.js"></script> <script src="/blog/js/mySelf.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script></div></body></html>